﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Configuration;
using CCWin;
using System.Data.SqlClient;

namespace weiqijiance
{
    public partial class user : CCSkinMain
    {
        public user()
        {
            InitializeComponent();
            getpic(pictureBox1, "./ico/用户.ico");
            getpic(pictureBox2, "./ico/密码.ico");
            getpic(pictureBox3, "./ico/用户登入2.ico");
            //this.BackgroundImage = Image.FromFile(@"./ico/登录背景.jpg");
            //this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }

        public void getpic(PictureBox pic, string slujing)
        {
            //得到文件名及路徑   
            string sPicPaht = slujing;
            //FileInfo：提供建立、複製、刪除、移動和開啟檔案的執行個體 (Instance) 方法   
            FileInfo fiPicInfo = new FileInfo(sPicPaht);
            //Length：取得目前檔案的大小。以字節為單位   
            long lPicLong = fiPicInfo.Length / 1024;
            //得到文名   
            string sPicName = fiPicInfo.Name;
            //取得父目錄   
            string sPicDirectory = fiPicInfo.Directory.ToString();
            //DirectoryName ：取得表示目錄完整路徑。   
            string sPicDirectoryPath = fiPicInfo.DirectoryName;
            //封裝GDI+點陣圖像，是用來處理像素資料所定義影像的物件。   
            //Bitmap類：封裝GDI+ 點陣圖，這個點陣圖是由圖形影像的像素資料及其屬性所組成。Bitmap 是用來處理像素資料所定義影像的物件。   
            Bitmap bmPic = new Bitmap(sPicPaht);
            //如果文件大於500KB，警告   
            if (lPicLong > 4000)
            {
                MessageBox.Show("此文件大小為" + lPicLong + "K；已超過最大限制的400K范圍！");
            }
            else
            {
                Point ptLoction = new Point(bmPic.Size);
                if (ptLoction.X > pic.Size.Width || ptLoction.Y > pic.Size.Height)
                {
                    //圖像框的停靠方式   
                    //pcbPic.Dock = DockStyle.Fill;   
                    //圖像充滿圖像框，並且圖像維持比例   
                    pic.SizeMode = PictureBoxSizeMode.Zoom;
                }
                else
                {
                    //圖像在圖像框置中   
                    pic.SizeMode = PictureBoxSizeMode.CenterImage;
                }
            }
            //LoadAsync：非同步載入圖像   
            pic.LoadAsync(sPicPaht);
           
        }

        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;
        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
        string sDataBaseIp;
        string sDataBaseName;
        string sDataBaseUserName;
        string sDataBasePwd;
        public string sDataBaseIpwq;
        public string sDataBaseNamewq;
        public string sDataBaseUserNamewq;
        public string sDataBasePwdwq;
        public void getSQLSZ()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\数据库设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            sDataBaseIp = ReadValue("设置", "数据库IP");
            sDataBaseName = ReadValue("设置", "数据库名称");
            sDataBaseUserName = ReadValue("设置", "用户");
            sDataBasePwd = ReadValue("设置", "密码");
            sDataBaseIpwq = ReadValue("尾气设置", "数据库IP");
            sDataBaseNamewq = ReadValue("尾气设置", "数据库名称");
            sDataBaseUserNamewq = ReadValue("尾气设置", "用户");
            sDataBasePwdwq = ReadValue("尾气设置", "密码");

        }
        public DataTable GetTable(string sSQL)
        {
            //getSQLSZ();
            DataTable dttt = new DataTable();
            try
            {
                string connectionstring = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};", sDataBaseIpwq, sDataBaseNamewq, sDataBaseUserNamewq, sDataBasePwdwq);
                SqlConnection sqlconn = new SqlConnection(connectionstring);
                sqlconn.Open();
                SqlCommand cmd = new SqlCommand(sSQL, sqlconn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                sqlconn.Close();
                return ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return dttt;
            }
        }
        private void pictureBox3_Click(object sender, EventArgs e)
        {
            if (com_yonghu.Text == "admin" || t_mima.Text == "nimda")
            {
                number = 0;
                baocuncishu();
                ruanjianbanben m_af = new ruanjianbanben();
                this.Hide();
                m_af.ShowDialog();
            }
            else
            {
                checkuser();
                //if (!inicbx(com_yonghu.Text, t_mima.Text))
                //{
                //    MessageBox.Show("用户名或密码错误！");
                //    return;
                //}
            }
            
        }
        public void checkuser()
        {
            readxml();
            if (com_yonghu.Text == "")
            {
                number++;
                baocuncishu();
                if (number > 2)
                {
                    MessageBox.Show("系统已锁止，请与检测站管理部门联系");
                }
                else
                {
                    MessageBox.Show("输入错误," + "输入" + (3 - number) + "次错误后锁止系统");
                }
                return;

            }
            else
            {
                if (t_mima.Text == "")
                {

                    number++;
                    baocuncishu();
                    if (number > 2)
                    {
                        MessageBox.Show("系统已锁止，请与检测站管理部门联系");
                    }
                    else
                    {
                        MessageBox.Show("输入错误," + "输入" + (3 - number) + "次错误后锁止系统");
                    }
                    return;
                }
                else
                {
                    if ((com_yonghu.Text != "gzwk") && (t_mima.Text != "87703242"))
                    {

                        string sql;
                        sql = string.Format(@"Select * From Employee where EP_ID='{0}' and EP_PASS='{1}'", com_yonghu.Text, t_mima.Text);
                        DataTable dt = GetTable(sql);
                        if (dt.Rows.Count < 1)
                        {
                            number++;
                            baocuncishu();
                            if (number > 2)
                            {
                                MessageBox.Show("系统已锁止，请与检测站管理部门联系");
                            }
                            else
                            {
                                MessageBox.Show("输入错误," + "输入" + (3 - number) + "次错误后锁止系统");
                            }
                            return;
                        }
                    }
                    else
                    {
                        if ((com_yonghu.Text != "gzwk") || (t_mima.Text != "87703242"))
                        {
                            number++;
                            baocuncishu();
                            if (number > 2)
                            {
                                MessageBox.Show("系统已锁止，请与检测站管理部门联系");
                            }
                            else
                            {
                                MessageBox.Show("输入错误," + "输入" + (3 - number) + "次错误后锁止系统");
                            }
                            return;
                        }
                    }
                }
            }
            if (number > 2)
            {
                if (com_yonghu.Text == "gzwk" && t_mima.Text == "87703242")
                {
                    number = 0;
                    baocuncishu();
                    ruanjianbanben m_af = new ruanjianbanben();
                    this.Hide();
                    m_af.ShowDialog();
                }
                else
                {
                    MessageBox.Show("系统已锁止，请与检测站管理部门联系");
                    return;
                }
            }
            else
            {
                number = 0;
                baocuncishu();
                ruanjianbanben m_af = new ruanjianbanben();
                this.Hide();
                m_af.ShowDialog();
            }
        
        
        }
        public int number;
        public void baocuncishu()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\数据库设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            Writue("设置", "次数", number.ToString());
        }
        public void readxml()
        {
            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\数据库设置.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);
            int.TryParse(ReadValue("设置", "次数"), out number);
        }

        bool inicbx(string cbx, string pwd)
        {
            bool breturn = false;
            string ssqlex = "";
            ssqlex = string.Format(@"select * from Employee where EP_ID='{0}' and EP_PASS='{1}'", cbx, pwd);
            if (GetTable(ssqlex).Rows.Count > 0)
            {
                breturn = true;
            }
            return breturn;
        }





        KeyboardHook ooo = new KeyboardHook();
        private void user_Load(object sender, EventArgs e)
        {
            getSQLSZ();
            //ooo.KeyMaskStart();
            if (sDataBaseIpwq.Trim().Length>0)
            {
            }
            else
            {
                mainform m_af = new mainform("");
                this.Hide();
                m_af.ShowDialog();
            }
        }

        private void user_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {
            //ooo.KeyMaskStop();
        }
    }
}
